<?php
/**
 * Created by PhpStorm.
 * User: dream
 * Date: 2017/8/20
 * Time: 16:09
 */
class Article extends MY_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->loadHelper("url");
    }

    //发布文章

    public function publish()
    {

        $sess_id = $this->session("user")->id;
        $this->loadModel("articletype");
        $types= $this->articletype->getWhere(['user_id'=>$sess_id],'','',true);
        $view_data['types'] = $types;

        $this->loadView(ADMIN.'article/article_publish',$view_data);
    }


    //获取到所有文章 进行分页显示
    public function index()
    {




        $this->loadView(ADMIN.'article/article_index');

    }

    //草稿箱列表
    public function draft()
    {

        $this->loadView(ADMIN.'article/article_draft');
    }

    //文章类型管理

    public function type()
    {

        $this->loadModel("articletype");
        $user_id = $this->session("user")->id;
        $offset = $this->uri->segment(4);
        if($this->isGet())
        {
            if(!empty($this->get('id')))
            {
                $this->articletype->delete(['user_id'=>$user_id,'id'=>$this->get('id')]);
            }

            $base_url = web_url('article/type',true);
            $types = $this->articletype->getWhere(['user_id'=>$user_id],$offset,self::PER_PAGE,true);
            $count = $this->articletype->getRows(['user_id'=>$user_id]);
            $views = [ADMIN.'article/article_type'];
            $view_data['types'] =  $types;
            $this->_pagination($base_url,$count,self::PER_PAGE,$views,$view_data);
        }
        else
        {
            $type= $this->post("type");
            if($type=="add")
            {
                $article_type = $this->post("type_name");
                $set['article_type'] = $article_type;
                $set['ctime']=$this->time();
                $set["user_id"] = $user_id;
                $add_res=  $this->articletype->add($set);
                $this->redirect(web_url('article/type',true));
            }else if($type=="search")
            {
                $key_word = $this->post("key_word");
                $where = ['article_type'=>$key_word,'user_id'=>$user_id];
                $types = $this->articletype->getWhere($where,$offset,self::PER_PAGE,true);
                $view_data['links']="";
                $view_data['types'] = $types;

                $this->loadView(ADMIN.'article/article_type',$view_data);
            }
        }

    }

    public function update_type()
    {

        $new_name = $this->post("new_name");
        $id = $this->post("id");
        $user_id = $this->session("user")->id;
        $this->loadModel("articletype");
        $this->articletype->update(['article_type'=>$new_name],['id'=>$id,'user_id'=>$user_id]);
    }


    //评论列表

    public function comments()
    {
       $this->loadView(ADMIN.'article/article_comments');
    }

    //浏览记录 日志

    public function  log()
    {

        $this->loadView(ADMIN.'article/article_log');
    }

}